/*
 * @Author: silence
 * @Date: 2023-07-04 15:42:53
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-01-04 10:49:23
 * @Description: 电站详情——基础信息
 */
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';

import '../../../base/SilAppBar.dart';
import '../../../base/SilScaffold.dart';
import '../../../common/widgets/cubitWidget/DividingLine.dart';
import '../../../utils/color/ColorUtils.dart';
import '../../Mine/Language/SilLocalizations/SilLocalizations.dart';
import '../Controller/PowerStationDetailController.dart';

class PsDetailBaseInfoView extends StatefulWidget {
  const PsDetailBaseInfoView({super.key});

  @override
  State<PsDetailBaseInfoView> createState() => _PsDetailBaseInfoViewState();
}

class _PsDetailBaseInfoViewState extends State<PsDetailBaseInfoView> {
  PowerStationDetailController _controller =
      Get.put(PowerStationDetailController());

  @override
  Widget build(BuildContext context) {
    return Obx(() => mainView());
  }

  mainView() {
    String? stationType = _controller.powerStationEntity.value.stationType;

    return Container(
      decoration: BoxDecoration(
          image: DecorationImage(
              image: AssetImage(stationType == '3'
                  ? 'imgs/home/home_bg_e3.png'
                  : 'imgs/home/home_bg.png'),
              fit: BoxFit.fitWidth,
              alignment: Alignment.topCenter)),
      child: Obx(() => contentView()),
    );
  }

  contentView() {
    return Container(
      margin: EdgeInsets.only(top: 24.w),
      child: Column(
        children: [
          /// 顶部导航
          topNavView(),

          /// 运行信息
          runInfoView(),

          /// 具体信息
          specificInfoView(),

          /// 分割线
          DividingLine(
            margin: EdgeInsets.symmetric(horizontal: 15.w, vertical: 30.w),
            color: ColorUtils.hexColor('#4a4a4a'),
          )
        ],
      ),
    );
  }

  /// 顶部信息
  topNavView() {
    return Container(
      margin: EdgeInsets.only(
          top: MediaQuery.of(context).padding.top + 20.w, bottom: 30.w),
      child: SilAppBar(
        backgroundColor: Colors.transparent,
        leadingWidget: SilScaffoldButton.backButtonStatic(context),
        titleWidget: SilScaffoldButton.titleView(
            title: _controller.powerStationEntity.value.ps_name),
      ),
    );
  }

  /// 运行信息
  runInfoView() {
    return Column(
      children: [
        SilText(LangOf(context)?.dz_zzw?.safely_run,
            color: ColorUtils.color_font_main, fontSize: 13.sp, maxLines: 0),
        Padding(padding: EdgeInsets.only(top: 10.w)),
        RichText(
            text: TextSpan(children: [
          TextSpan(
              text: _controller.powerStationEntity.value.runTime,
              style: TextStyle(
                  fontSize: 34.sp, color: ColorUtils.color_font_main)),
          TextSpan(
              text: LangOf(context)?.dz_zzw?.days,
              style: TextStyle(
                  fontSize: 19.sp, color: ColorUtils.color_font_main)),
        ]))
      ],
    );
  }

  /// 具体信息
  specificInfoView() {
    List<String?> itemTitles = [];
    List<String?> itemValues = ['0', '0', '0', '0'];

    /// 低压机型 显示总储能
    if (_controller.powerStationEntity.value.equipmentSelection != '1') {
      itemTitles.add(LangOf(context)?.dz_zzw?.generation); // 总发电
      itemTitles.add(LangOf(context)?.dz_zzw?.income); // 总收益
      itemTitles.add(LangOf(context)?.dz_zzw?.energy_storing); // 总储能
      itemTitles.add(LangOf(context)?.dz_zzw?.totalEnergy); // 总用电

      itemValues = ['0', '0', '0', '0'];

      itemValues[0] =
          _controller.powerStationEntity.value.generation_total; // 总发电
      itemValues[1] = _controller.powerStationEntity.value.money_total; // 总收益
      itemValues[3] = _controller.powerStationEntity.value.stored_total; // 总储能
      itemValues[2] =
          _controller.powerStationEntity.value.use_electric_total; // 总用电
    } else {
      itemTitles.add(LangOf(context)?.dz_zzw?.generation); // 总发电
      itemTitles.add(LangOf(context)?.dz_zzw?.income); // 总收益
      itemTitles.add(LangOf(context)?.dz_zzw?.totalEnergy); // 总用电
      itemValues = ['0', '0', '0'];

      itemValues[0] =
          _controller.powerStationEntity.value.generation_total; // 总发电
      itemValues[1] = _controller.powerStationEntity.value.money_total; // 总收益
      itemValues[2] =
          _controller.powerStationEntity.value.use_electric_total; // 总用电
    }

    List<Widget> itemWidgets = [];
    for (var i = 0; i < itemTitles.length; i++) {
      SilText titleWidget = SilText(
        itemTitles[i],
        color: ColorUtils.color_font_main,
        fontSize: 13.sp,
        maxLines: 0,
        textAlign: TextAlign.center,
      );

      String valueStr;
      if (i == 1) {
        // 价格
        valueStr =
            "${_controller.powerStationEntity.value.moneyUnit}${itemValues[i]}";
      } else {
        valueStr = "${itemValues[i]}kWh";
      }

      /// 收益，判断一下电价为0时，显示图片
      Widget valueWidget = SilText(
        valueStr,
        color: ColorUtils.color_font_main,
        fontSize: 13.sp,
        maxLines: 0,
        textAlign: TextAlign.center,
      );

      itemWidgets.add(Expanded(
          child: SilTextIconButton(
        titleWidget,
        valueWidget,
        space: 20.w,
        imagePosition: ImagePosition.Down,
      )));
    }

    return Container(
      padding: EdgeInsets.symmetric(horizontal: 5.w),
      margin: EdgeInsets.only(top: 233.w, right: 15.w, left: 15.w),
      child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: itemWidgets),
    );
  }
}
